import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:lucky_orbs/presentation/super_lotto/bloc/super_lotto_bloc.dart';
import 'package:lucky_orbs/presentation/super_lotto/bloc/super_lotto_event.dart';
import 'package:lucky_orbs/presentation/super_lotto/bloc/super_lotto_state.dart';
import 'package:lucky_orbs/presentation/super_lotto/widgets/lottery_animation_view.dart';
import 'package:lucky_orbs/presentation/super_lotto/widgets/result_display.dart';
import 'package:lucky_orbs/presentation/super_lotto/widgets/ticket_display.dart';

class SuperLottoPage extends StatelessWidget {
  const SuperLottoPage({super.key});

  @override
  Widget build(BuildContext context) {
    return BlocProvider(
      create: (_) => SuperLottoBloc(),
      child: const SuperLottoView(),
    );
  }
}

class SuperLottoView extends StatelessWidget {
  const SuperLottoView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('超级大乐透模拟器'),
        backgroundColor: Colors.transparent,
        elevation: 0,
        centerTitle: true,
      ),
      body: Center(
        child: BlocBuilder<SuperLottoBloc, SuperLottoState>(
          builder: (context, state) {
            if (state is SuperLottoDrawing) {
              return const LotteryAnimationView();
            }

            if (state is SuperLottoFinished) {
              return ResultDisplay(state: state);
            }

            return const TicketSelectionView();
          },
        ),
      ),
    );
  }
}

class TicketSelectionView extends StatelessWidget {
  const TicketSelectionView({super.key});

  @override
  Widget build(BuildContext context) {
    final ticketState = context.watch<SuperLottoBloc>().state;
    final bool hasTicket = ticketState is SuperLottoTicketReady;

    return Padding(
      padding: const EdgeInsets.all(20.0),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          const Text(
            '幸运号码选择',
            style: TextStyle(fontSize: 28, color: Colors.white70, fontWeight: FontWeight.bold),
          ),
          if (hasTicket)
            TicketDisplay(ticket: (ticketState as SuperLottoTicketReady).ticket)
          else
            const Text('点击 "随机一注" 生成您的幸运号码', style: TextStyle(color: Colors.white54)),
          Column(
            children: [
              ElevatedButton(
                onPressed: () => context.read<SuperLottoBloc>().add(GenerateRandomTicket()),
                child: const Text('随机一注'),
              ),
              const SizedBox(height: 20),
              ElevatedButton(
                onPressed: hasTicket
                    ? () => context.read<SuperLottoBloc>().add(StartDrawing((ticketState as SuperLottoTicketReady).ticket))
                    : null,
                style: ElevatedButton.styleFrom(
                  backgroundColor: hasTicket ? const Color(0xFF00D1FF) : Colors.grey, // 霓虹蓝色
                ),
                child: const Text('开始摇奖'),
              ),
            ],
          ),
        ],
      ),
    );
  }
}